package com.press.controllers.blog;

import com.alibaba.fastjson.JSONObject;
import com.press.bean.ContentBean;
import com.press.bean.TaxonomyBean;
import com.press.beanforview.VContentBean;
import com.press.services.DetailsService;
import com.press.services.SearchService;
import com.press.services.UtilService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created by haocongshun on 2016/10/2.
 */
@Controller
@RequestMapping("/blog")
public class ContentController {
    @Resource(name = "detailsService")
    private DetailsService detailsService;
    @Resource(name = "searchService")
    private SearchService searchService;

    public DetailsService getDetailsService() {
        return detailsService;
    }

    public void setDetailsService(DetailsService detailsService) {
        this.detailsService = detailsService;
    }

    public SearchService getSearchService() {
        return searchService;
    }

    public void setSearchService(SearchService searchService) {
        this.searchService = searchService;
    }

    @RequestMapping("/content")
    public void content(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        UtilController utilController = (UtilController) WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()).getBean("utilController");
        utilController.contentsRecommend(request);
        utilController.contentsTop10(request);
        utilController.menus(request);

        //获取application中的配置
        Map<String,String> option = (Map<String ,String>)request.getServletContext().getAttribute("Option");

        //根据id获取当前文章内容
        String content_id = request.getParameter("content_id");
        VContentBean vContent = new VContentBean();
        ContentBean contentBean = detailsService.getContentById(content_id);
        if(contentBean!=null){
            vContent.setTitle(contentBean.getContent_title());
            vContent.setSummary(contentBean.getContent_summary());
            vContent.setThumbnail(contentBean.getContent_thumbnail());
            vContent.setCreateDate(contentBean.getContent_created());
            vContent.setAuthor(contentBean.getContent_author());
            vContent.setText(contentBean.getContent_text());
            vContent.setModifiedDate(contentBean.getContent_modified());
            vContent.setWatched(contentBean.getContent_watched());
        }else {
            request.getRequestDispatcher("/"+option.get("web_template_id")+"/content.jsp").forward(request,response);
            return;
        }


        //根据id获取分类
        List<TaxonomyBean> typeList = searchService.gettaxonomybycontent_id(content_id);
        Map<String,String> taxonomy = new LinkedHashMap<>();
        for(int j = 0 ; j < typeList.size();j++){
            if(typeList.get(j).getTaxonomy_type().equals(contentBean.getContent_id())){
                taxonomy.put(request.getServletContext().getAttribute("URLTaxonomy")+"?tax="+typeList.get(j).getTaxonomy_id(),typeList.get(j).getTaxonomy_title());
            }
        }
        vContent.setTaxonomys(taxonomy);
        vContent.setUrl(request.getServletContext().getAttribute("URLContent")+"?content_id="+contentBean.getContent_id());
        request.setAttribute("content",vContent);
        request.setAttribute("title",vContent.getTitle());


        //根据id获取相关文章
        List<VContentBean> contentsRelated = new ArrayList<VContentBean>();
        for (ContentBean content :detailsService.getRelated(content_id)){
            VContentBean vContentre = new VContentBean();
            vContentre.setTitle(content.getContent_title());
            vContentre.setUrl(request.getServletContext().getAttribute("URLContent")+"?content_id="+content.getContent_id());
            contentsRelated.add(vContentre);
        }
        request.setAttribute("contentsRelated",contentsRelated);






        //根据id获取上一篇文章
        VContentBean Contentlast = new VContentBean();
        ContentBean lastContent = detailsService.getLastContent(content_id);
        if(lastContent==null){
            Contentlast.setTitle("当前已是第一篇");
            Contentlast.setUrl("");
        }else{
            Contentlast.setTitle(lastContent.getContent_title());
            Contentlast.setSummary(lastContent.getContent_summary());
            Contentlast.setThumbnail(lastContent.getContent_thumbnail());
            Contentlast.setUrl(request.getServletContext().getAttribute("URLContent")+"?content_id="+lastContent.getContent_id());
        }
        request.setAttribute("lastContent",Contentlast);


        //根据id获取下一篇文章
        VContentBean Contentnext = new VContentBean();
        ContentBean nextContent = detailsService.getNextContent(content_id);
        if(nextContent==null){
            Contentnext.setTitle("当前已是最后一篇");
            Contentnext.setUrl("");
        }else{
            Contentnext.setTitle(nextContent.getContent_title());
            Contentnext.setSummary(nextContent.getContent_summary());
            Contentnext.setThumbnail(nextContent.getContent_thumbnail());
            Contentnext.setUrl(request.getServletContext().getAttribute("URLContent")+"?content_id="+nextContent.getContent_id());
        }
        request.setAttribute("nextContent",Contentnext);


        request.getRequestDispatcher("/"+option.get("web_template_id")+"/content.jsp").forward(request,response);
    }

}
